// index.js
import util from '../../../utils/util.js';
var app = getApp();
var logincontent = '尚未登录，请先登录吗？';
import regeneratorRuntime from '../../../utils/regenerator-runtime/runtime.js'
import { getObjectFromStorgeSync, KEYS } from '../../../utils/http.js';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    num_czk:'0',
    num_tck:'0',
    num_yhq:'0',
    num_gd_uncommennted:2,//未评论的已完成工单
    username:'',
    userimg:'',
    visible3: false,
    islogin:false,
    user: '',
    carcode:'',
    services:[],
    actions3: [
      {
        name: '退出登录',
        color: '#2d8cf0',
      },
      {
        name: '取消'
      }
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  // 登出账号
  logout() {
    var that = this;
    wx.showModal({
      title: '提醒',
      content: '确定退出登录吗？',
      success: function (eci) {
        if (eci.confirm) {
          // wx.setStorageSync('user_info', '');
          util.saveData('userinfo','');
          that.setData({
            username: '登录',
            userimg: '/images/appIcon.png',
            islogin:false,
          });
        } else {

        }
      }
    });
  },
  // jump

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  login:function(){
    if(!this.data.islogin)
    {
    wx.navigateTo({
      url: '../../auth/login/login'
    })
    }
  },

  member_about: function () {
    // if (!this.data.islogin) {
      wx.navigateTo({
        url: '../../util/webview/webview?url=https://www.autorepairehelper.cn/noticeboard/about'
      })
    // }
  },
  
  intro:function(){
    wx.navigateTo({
      url: '../../util/webview/webview?url=https://www.autorepairehelper.cn/noticeboard/xcxsysc'
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    
    var that = this

    this.getMyVipCard()
    var usetinfo = util.getData("userinfo");

    if (usetinfo == "") {
      this.setData({
        username: '登录',
        userimg: '/images/appIcon.png',
        islogin: false,
      });

    } else {
      this.setData({
        username: usetinfo.name,
        userimg: usetinfo.headurl,
        islogin: true,
      });

      wx.getStorage(
        {
          key: 'defaultcar',
          success: function (res) {
            wx.setNavigationBarTitle({
              title:'当前车辆'+res.data.carcode,
              
            })
            that.setData({ carcode: res.data.carcode})
          }
        })
    }
    that.getRedNum()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  mycar: function () {
    if (this.data.islogin) {
      wx.navigateTo({
        url: '../../mycar/list/list'
      })
    }else{
      var that = this;
      wx.showModal({
        title: '提醒',
        content: logincontent,
        success: function (eci) {
          if (eci.confirm) {
            wx.navigateTo({
              url: '../../auth/login/login'
            })
          } else {
          }
        }
      });
    }
  },

  myschedulelist: function () {
    if (this.data.islogin) {
      wx.navigateTo({
        url: '../../schedule/schedulelist/schedulelist'
      })
    }else{
      var that = this;
      wx.showModal({
        title: '提醒',
        content: logincontent,
        success: function (eci) {
          if (eci.confirm) {
            wx.navigateTo({
              url: '../../auth/login/login'
            })
          } else {
          }
        }
      });
    }
  },

  myOrder: function () {
    if (this.data.islogin) {
    wx.navigateTo({
      url: '../../workorder/workorderlist/workorderlist'
    });
    }else{
      var that = this;
      wx.showModal({
        title: '提醒',
        content: logincontent,
        success: function (eci) {
          if (eci.confirm) {
            wx.navigateTo({
              url: '../../auth/login/login'
            })
          } else {
          }
        }
      });
    }
  },
  
  myFav: function () {
    if (this.data.islogin) {
      wx.navigateTo({
        url: '../../carstore/favlist/list'
      });
    } else {
      var that = this;
      wx.showModal({
        title: '提醒',
        content: logincontent,
        success: function (eci) {
          if (eci.confirm) {
            wx.navigateTo({
              url: '../../auth/login/login'
            })
          } else {
          }
        }
      });
    }
  },

  myMoneyCard: function () {
  
  },
  
  mySetCard: function () {
      var that = this;
    wx.navigateTo({
      url: '../servivecard/servicecard?services='+JSON.stringify(that.data.services)
    })
  },

  myCoupon: function () {
    wx.requestPayment(
      {
        'timeStamp': '',
        'nonceStr': '',
        'package': '',
        'signType': 'MD5',
        'paySign': '',
        'success': function (res) { },
        'fail': function (res) { },
        'complete': function (res) { }
      })
  },
  
  myPayHistory: function () {
    var that = this;
    wx.navigateTo({
      url: '../cardhistory/cardhistory'
    })
  },

  myWriteOff: function () {
    if (this.data.islogin) {
      wx.showToast({
        title: '敬请期待!',
      })
    } else {
      var that = this;
      wx.showModal({
        title: '提醒',
        content: logincontent,
        success: function (eci) {
          if (eci.confirm) {
            wx.navigateTo({
              url: '../../auth/login/login'
            })
          } else {
          }
        }
      });
    }
  },

  getMyVipCard:async function(){
      var that = this;
     const shop = await getObjectFromStorgeSync(KEYS.SHOP)
      var defaultcar = util.getData("defaultcar")
      var url = app.globalData.SERVER + 'vipCard/oneCotactVipCard';
      util.request2(url, 'POST', {
        owner: shop.tel,
        carcode: defaultcar.carcode,
        tel: defaultcar.tel,
      }).then(res => {
        that.setData({ num_czk: res.ret.totalMoney,services:res.ret.packageItems})
      }).catch(err => {
        that.setData({ num_czk: 0, services: []})
      });
  },

  getRedNum:async function() {
    var that = this;
    var url = app.globalData.SERVER + 'repair/queryAllWithStateByCarer3';
    const shop = getObjectFromStorgeSync(KEYS.SHOP)
    var usetinfo = util.getData("userinfo");
    var usetinfo_id = usetinfo._id;
    var defaultCar = util.getData("defaultcar");
    const carcode = defaultCar.carcode
    var status = this.data.type;
    util.request(url, 'POST', {
      tel: shop.tel,
      state: '2',
      iscommented:'0',
      carerid: usetinfo_id,
      shopid: shop._id,
      carcode: carcode
    }).then(res => {
      
    }).catch(err => {
      if (err.data.code === 1) {
        that.setData({num_gd_uncommennted:err.data.ret.length})
      } 
    });
  }
});